package com.sllp.C71S3TsllpShop.web.back;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Notice;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.NoticeBizImpl;

@Controller
public class BackNoticeListAction {

	@Resource
	private NoticeBizImpl nbi;
	
	@GetMapping("back/toNoticeList.do")
	public String toNoticeList(@SessionAttribute(name="loginUser",required=false) User user,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,
			@SessionAttribute(name="backLoginAdmin",required=false) Admin admin,
			Model m) {
		
		if(user != null && business != null) {
			//商家登录 只显示发布给商家的
			Notice notice = new Notice();
			notice.setWho(2);
			List<Notice> notices = nbi.findNotice(notice, 1, 2);
			m.addAttribute("noticeList", notices);
		}else if(admin != null){
			// 管理员登录   只显示自己发布的和发布给管理员的  
			// 先找到发给管理员的然后再把自己发布的找到，然后去重, 怎么分页查询？解：自己写sql
			Notice notice = new Notice();
			notice.setAdmin(admin.getId());
			notice.setWho(1);
			List<Notice> notices = nbi.findNotice(notice, 1, 2);
			m.addAttribute("noticeList", notices);
			m.addAttribute("loginAdmin", admin);
		}
		return "back/notice-list::noticeList";
	}
	
	@ResponseBody
	@PostMapping("back/showAdminNoticeDetils.do")
	public Notice showAdminNoticeDetils(Notice notice) {
		return nbi.findNotice(notice, null, null).get(0);
	}
	
	@PostMapping("back/toModifyNotice.do")
	public String toModifyNotice(Notice notice,Model m) {
		m.addAttribute("notice", nbi.findNotice(notice, null, null).get(0));
		return "back/notice-write::noticeWrite";
	}
	
	@PostMapping("back/noticeShowToPage.do")
	public String noticeShowToPage(int page,@SessionAttribute(name="loginUser",required=false) User user,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,
			@SessionAttribute(name="backLoginAdmin",required=false) Admin admin,
			Model m) {
		if(user != null && business != null) {
			//商家登录 只显示发布给商家的
			Notice notice = new Notice();
			notice.setWho(2);
			List<Notice> notices = nbi.findNotice(notice, page, 2);
			m.addAttribute("noticeList", notices);
		}else if(admin != null){
			// 管理员登录   只显示自己发布的和发布给管理员的  
			// 先找到发给管理员的然后再把自己发布的找到，然后去重, 怎么分页查询？解：自己写sql
			Notice notice = new Notice();
			notice.setAdmin(admin.getId());
			notice.setWho(1);
			List<Notice> notices = nbi.findNotice(notice, page, 2);
			m.addAttribute("noticeList", notices);
			m.addAttribute("loginAdmin", admin);
		}
		
		return "back/notice-list::noticeList";
	}
	
}
